package com.ys.peony.ui.widget;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

/**
 * Created by 59153 on 2017/9/30.
 */

public class MyClickableTextView extends TextView {

    public MyClickableTextView(Context context) {
        super(context);
        this.setOnTouchListener(VIEW_TOUCH_DARK);
    }

    public MyClickableTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.setOnTouchListener(VIEW_TOUCH_DARK);
    }

    public MyClickableTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setOnTouchListener(VIEW_TOUCH_DARK);
    }

    public final OnTouchListener VIEW_TOUCH_DARK = new OnTouchListener() {
        //变暗(三个-50，值越大则效果越深)
        public final float[] BT_SELECTED_DARK = new float[]{1, 0, 0, 0, -50, 0, 1,
                0, 0, -50, 0, 0, 1, 0, -50, 0, 0, 0, 1, 0};

        //变亮
        public final float[] BT_SELECTED_LIGHT = new float[]{1, 0, 0, 0, 50, 0, 1,
                0, 0, 50, 0, 0, 1, 0, 50, 0, 0, 0, 1, 0};
        //恢复
        public final float[] BT_NOT_SELECTED = new float[]{1, 0, 0, 0, 0, 0,
                1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0};

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    (v).setAlpha(0.5f);
                    break;
                case MotionEvent.ACTION_UP:
                    (v).setAlpha(1f);
                    performClick();
                    break;
                case MotionEvent.ACTION_CANCEL:
                    (v).setAlpha(1f);
                    break;
            }
            //true:执行ACTION_DOWN后会再往下执行move和up  false:ACTION_DOWN后不执行其他操作clicklistener会执行
            return true;
        }
    };

    public void setMyclickable(Boolean isClickable) {
        if(isClickable) {
            this.setOnTouchListener(VIEW_TOUCH_DARK);
            this.setClickable(true);
        } else {
            this.setOnTouchListener(null);
            this.setClickable(false);
        }
    }
}
